package org.sang.controller;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.sang.bean.ConsumeItemConfig;
import org.sang.dto.ConsumeTypeRepDto;
import org.sang.service.IConsumeItemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 消费配置.
 * 〈功能详细描述〉
 *
 * @author Naughty
 * @version 2018/4/4
 * @see ConsumeController
 * @since 1.0
 */
@RestController
@RequestMapping("/consume")
public class ConsumeController {
    @Autowired
    IConsumeItemConfigService consumeItemConfigService;

    @PostMapping(value = "/add", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "新增消费项目配置",notes = "新增消费项目配置")
    public ResponseEntity<ConsumeItemConfig> addConsumeItemConfig(@RequestBody ConsumeItemConfig consumeItemConfig) {
        return ResponseEntity.ok(consumeItemConfigService.insertConsumeItemConfig(consumeItemConfig));
    }

    @DeleteMapping(value = "/delete/{itemId}")
    @ApiOperation(value = "删除消费项目配置",notes = "删除消费项目配置")
    public ResponseEntity deleteConsumeItemConfig(
        @ApiParam(value = "项目编号",required = true,name = "itemId")
        @PathVariable("itemId") String itemId) {
        consumeItemConfigService.deleteConsumeItemConfig(itemId);
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/update",produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "更新消费项目配置",notes = "更新消费项目配置,根据门店,项目编号更新项目信息")
    public ResponseEntity<ConsumeItemConfig> updateConsumeItemConfig(
        @RequestBody ConsumeItemConfig consumeItemConfig){
        return ResponseEntity.ok(consumeItemConfigService.updateConsumeItemConfig(consumeItemConfig));
    }

    @GetMapping(value = "/query/one/{itemId}")
    @ApiOperation(value = "获取消费项目配置",notes = "获取消费项目配置,根据项目编号获取项目信息")
    public ResponseEntity<ConsumeItemConfig> queryConsumeItemConfig(
        @ApiParam(value = "项目编号",required = true,name = "itemId")
        @PathVariable("itemId") String itemId){
        return ResponseEntity.ok(consumeItemConfigService.queryConsumeItemConfig(itemId));
    }

    @PostMapping(value = "/query/list")
    @ApiOperation(value = "获取门店消费项目配置列表,翻页",notes = "获取门店消费项目配置列表,翻页")
    public ResponseEntity<Map<String,Object>> queryConsumeItemConfigList(
        @RequestBody ConsumeItemConfig consumeItemConfig,
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize){

        Map<String,Object> resultMap= Maps.newHashMap();
        Page<ConsumeItemConfig> page = consumeItemConfigService.queryConsumeItemConfigListByPage(pageNum,pageSize,consumeItemConfig);
        resultMap.put("result", page.getResult());
        resultMap.put("total", page.getTotal());
        return ResponseEntity.ok(resultMap);
    }
    @GetMapping(value = "/query/list/{storeId}")
    @ApiOperation(value = "获取某门店消费项目配置列表",notes = "获取某门店消费项目配置列表")
    public ResponseEntity<List<ConsumeTypeRepDto>> findConsumeItemConfigByStoreId(
        @ApiParam(value = "门店编号",required = true,name = "门店编号")
        @PathVariable("storeId") String storeId){
            return ResponseEntity.ok(consumeItemConfigService.findConsumeItemConfigByStoreId(storeId));
    }
}
